﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Gola.VN.Scb.Web.Common.Entities;
using Gola.VN.Scb.Web.Dal;

namespace Gola.VN.Scb.Web.Bll
{
    internal class ProviderBiz : BaseBiz
    {
        internal ProviderBiz(MainDao mainDao)
            : base(mainDao)
        {
        }

        internal void InitializeData()
        {
            // 1. get data from database
            var package = new DaoPackage();
            package.Channel = ChannelType.Sql;
            package.Event = DaoEvent.GetProviders;
            _mainDao.Execute(package);

            // 2. set data into memory
            package.Channel = ChannelType.Memory;
            package.Event = DaoEvent.SetProviders;
            package.Input = package.Output;
            _mainDao.Execute(package);
        }

        public int InsertProvider(ProviderEntity provider)
        {
            using (var sqlCmm = new SqlCommand(QueryConstants.Provider.Insert))
            {
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.ExpressionId),
                                               provider.ExpressionId);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Name), provider.Name);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.DisplayName),
                                               provider.DisplayName);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Address), provider.Address);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Email), provider.Email);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.PhoneNumber),
                                               provider.PhoneNumber);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Logo), provider.Logo);

                return ProductDal.ExecuteNonQuery(sqlCmm);
            }
        }

        public int UpdateProvider(ProviderEntity provider)
        {
            using (var sqlCmm = new SqlCommand(QueryConstants.Provider.Update))
            {
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Name), provider.Name);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.DisplayName),
                                               provider.DisplayName);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Address), provider.Address);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Email), provider.Email);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.PhoneNumber),
                                               provider.PhoneNumber);
                sqlCmm.Parameters.AddWithValue(string.Format("@{0}", QueryConstants.Provider.Logo), provider.Logo);

                return ProductDal.ExecuteNonQuery(sqlCmm);
            }
        }

        public List<ProviderEntity> GetAllProviders()
        {
            var package = new DaoPackage
                              {
                                  Channel = ChannelType.Memory,
                                  Event = DaoEvent.GetProviders
                              };
            _mainDao.Execute(package);

            var providers = package.Output as IEnumerable<ProviderEntity>;

            // Set location to providers
            //1. get all location 
            if (providers == null)
            {
                return new List<ProviderEntity>();
            }


            foreach (var item in providers)
            {
                item.ProviderLocations = new LocationBiz(_mainDao).GetLocationByProviderId(item.Id);
            }

            return providers.ToList();
        }

        public ProviderEntity GetProvider(string name)
        {
            List<ProviderEntity> providers = GetAllProviders();
            return providers.FirstOrDefault(p => p.Name == name && p.ExpressionId == ProductsBll.VirtualExpressionId);
        }

        public ProviderEntity GetProvider(int id)
        {
            List<ProviderEntity> providers = GetAllProviders();
            return providers.FirstOrDefault(p => p.Id == id);
        }

        private ProviderEntity GetProviderLocation(int providerId)
        {
            //1. Get all Locations
            var package = new DaoPackage
                              {
                                  Channel = ChannelType.Memory,
                                  Event = DaoEvent.GetLocations
                              };
            _mainDao.Execute(package);

            var locations = package.Output as IEnumerable<LocationEntity>;
            //2. Get Provider by Id
            package.Event = DaoEvent.GetProviders;
            _mainDao.Execute(package);
            ProviderEntity provider =
                (package.Output as IEnumerable<ProviderEntity>).FirstOrDefault(q => q.Id == providerId);
            //3. Set locations to provider
            if (locations != null)
            {
                provider.ProviderLocations = locations.Where(q => q.ProviderId == providerId);
            }

            return provider;
        }
    }
}